package cn.com.controller.manageController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Surface;
import cn.com.db.service.SurfaceService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Auth(UserAuth.ADMIN)
@RequestMapping("/back/surface")
@RestController
public class SurfaceManageController {

    @Autowired
    SurfaceService surfaceService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createSurface(Surface surface){
        DataResult dataResult=new DataResult();
        if (StringUtils.isNotBlank(surface.getParent())&&
                !surfaceService.isExist(surface.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        surface.setId(UidUtil.createUUID());
        surface.setCreated_at(TimeUtil.getNowTimestamp());
        surface.setUpdated_at(TimeUtil.getNowTimestamp());
        surfaceService.createSurface(surface);
        dataResult.setData(surface);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String getSurface(@RequestParam("parent")String parent){
        DataResult dataResult=new DataResult();
        dataResult.setTotal(surfaceService.countSurface(parent));
        dataResult.setData(surfaceService.getSurface(parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String viewSurface(@PathVariable("id")String id){
        DataResult dataResult=new DataResult();
        if (!surfaceService.isExist(id)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("表层不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setData(surfaceService.getSurfaceById(id));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Surface surface){
        DataResult dataResult=new DataResult();
        if (!surfaceService.isExist(surface.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("表层不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        if (StringUtils.isNotBlank(surface.getParent())&&
                !surfaceService.isExist(surface.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        surfaceService.updateSurface(surface);
        dataResult.setData(surfaceService.getSurfaceById(surface.getId()));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
